package com.itheima.test.TCP.fileuploading;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Service {//服务器接受文件,并保存到服务器

    public static void main(String[] args) throws IOException {
        //创建网络中的流
        ServerSocket serverSocket = new ServerSocket(1000);
        Socket socket = serverSocket.accept();
        InputStream in = socket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(in);

        //创建本地流
        FileOutputStream fos = new FileOutputStream("day020\\b.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        //读写数据
        int ch;
        while ((ch = bis.read()) != -1) {
            bos.write(ch);//将网络中的数据写到本地
        }

        //传回上传成功
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF-8"));
        bw.write("上传成功");
        bw.newLine();
        bw.flush();

        //释放资源,网络中的流不用管,关本地的流
        bos.close();
        fos.close();
        socket.close();
        serverSocket.close();
    }
}
